package com.lyl.syt.modules.mongodb.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lyl.syt.model.hosp.Department;
import com.lyl.syt.modules.mongodb.dao.DepartmentDao;
import com.lyl.syt.modules.mongodb.service.DepartmentService;
import com.lyl.syt.vo.hosp.DepartmentQueryVo;
import com.lyl.syt.vo.hosp.DepartmentVo;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class DepartmentServiceImpl implements DepartmentService {
    private final DepartmentDao departmentDao;


    @Override
    public void save(Map<String, Object> paramMap) {
        //将map转换成实体类
        String json = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(json, Department.class);
        //根据医院编号和科室编号进行查询
        Department dept = departmentDao.getByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        if (dept == null) {
            department.setCreateTime(new Date());
        }
        department.setUpdateTime(new Date());
        department.setIsDeleted(0);
        departmentDao.save(department);
    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo queryVo) {
        //创建pageAble对象，设置分页参数
        Pageable pageable = PageRequest.of(page - 1, limit);

        //创建example对象
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        //构建查询对象
        Department department = new Department();
        department.setHoscode(queryVo.getHoscode());
        department.setIsDeleted(0);
        Example<Department> example = Example.of(department, matcher);
        return departmentDao.findAll(example, pageable);
    }

    @Override
    public void remove(String hoscode, String depcode) {
        //根据医院编号和科室编号查询科室信息
        Department department = departmentDao.getByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            //删除科室
            departmentDao.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> resultList = new ArrayList<>();

        //根据医院编号查询医院所有的科室信息
        Department department = new Department();
        department.setHoscode(hoscode);
        //所有科室的列表信息
        List<Department> deptList = departmentDao.findAll(Example.of(department));

        //根据大科室编号 bigcode 分组，获取每个大科室里面下级子科室
        Map<String, List<Department>> listMap = deptList.stream()
                .collect(Collectors.groupingBy(Department::getBigcode));

        listMap.forEach((k, v) -> {
            //封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(k);
            departmentVo.setDepname(v.get(0).getBigname());

            //封装小科室数据
            List<DepartmentVo> childList = v.stream()
                    .map(t -> {
                        DepartmentVo target = new DepartmentVo();
                        target.setDepcode(t.getDepcode());
                        target.setDepname(t.getDepname());
                        return target;
                    }).collect(Collectors.toList());
            departmentVo.setChildren(childList);

            //添加到返回集合
            resultList.add(departmentVo);
        });

        return resultList;
    }

    @Override
    public Department getByHoscodeAndDepcode(String hoscode, String depcode) {
        return departmentDao.getByHoscodeAndDepcode(hoscode,depcode);
    }
}
